import { Component, OnInit } from '@angular/core';
import { Router,ActivatedRoute }   from '@angular/router';
import { MdDialog } from '@angular/material';

import { Hospital }               from '../../domain/hospital.mdl';
import { HospitalDialogForm } from './components/hospital.dialog.form';

import { AuthService }      from '../../services/auth.service';
import { HospitalService }  from '../../services/hospital.service';


var DlgConfig = {
  disableClose:true,
  hasBackdrop:true,
  width:'680px',
  height:'520px'
};

@Component({
  selector: 'hospital-list',
  templateUrl: 'hospital.component.html',
  styles: [
    `.clkt {
      cursor: pointer;
    }
    .licon{
      width:24px;
      height:24px;
    }
    #locListPanel{
      min-height:360px;
    }
    `
  ]
})
export class HospitalComponent implements OnInit {

  hospitals: Hospital[];
  role:string;
  isMonitor:boolean = true;
  selectedHospital: Hospital;

  constructor(private route: ActivatedRoute,private router: Router,
    private hospitalService: HospitalService,
    private authService:AuthService,
    public dialog: MdDialog) {
    //console.log('HospitalComponent constructor calle.');
    //console.log(moment().format('YYYY-MM-DD HH:mm:ss'),'called.');
  }

  getHospitals(userInfo:any): void {
    //console.log('getHospitals called');
    this.hospitalService.getHospitals(userInfo).subscribe(
      hospitals => {
        this.hospitals = hospitals;
      },
      error =>{
        layer.msg('load data failed.'+error);
      }
    );
  }
  
  refreshList():void {
    var userInfo = this.authService.getUserInfo();
    //console.log('userInfo='+JSON.stringify(userInfo,null,2));
    this.role = userInfo.role;
    this.isMonitor = userInfo.role == 'view';
    //console.log('isMonitor='+this.isMonitor);
    this.getHospitals(userInfo);
  }

  ngOnInit(): void {
    //console.log('HospitalComponent:ngOnInit() called.');
    this.refreshList();
    //console.log('this.isMonitor=',this.isMonitor);
  }
  
  onHospitalDeleted(lid: string) {
    this.hospitals = this.hospitals.filter(function(hospital){
      return hospital._id != lid;
    });
  }

  deleteHospital(lid:string):void {
    //locMgr.deleteHospital(lid);
    //console.log('cid='+cid);
    layerHelper.confirm('Are you sure to delete this Record?', 'Delete Confirm',
      (index)=>{
      this.hospitalService.deleteHospital(lid).subscribe(
        delId =>{
          //console.log('delId='+delId);
          this.onHospitalDeleted(delId);
          layer.msg('delete over.');
          //layer4ng.close(index);
        },
        error =>{
          console.log('error='+error);
          layer.msg(error);
        }
      );
    });
  }
  
  openAddDlg():void {
    let dialogRef = this.dialog.open(
      HospitalDialogForm,DlgConfig);

    dialogRef.afterClosed().subscribe(result => {
      console.log(JSON.stringify(result,null,2));
      if(result)
        //this.onHospitalAdded.emit(result);
        this.hospitals.unshift(result);
    });
  }

  openEditDlg(formData:any):void {
    let dialogRef = this.dialog.open(HospitalDialogForm,
      $.extend({}, DlgConfig,{data:formData}));

    dialogRef.afterClosed().subscribe(result => {
      //this.selectedOption = result;
      console.log(`Dialog result: ${result}`);
    });
  }
}

